﻿using Furion;
using Furion.FriendlyException;
using Furionss.Application;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Furionss.Web.Entry.Controllers
{
    /// <summary>
    /// Default控制器
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]

    //[ApiDescriptionSettings("Group1")]
    public class DefaultController : ControllerBase
    {

        private readonly AppInfoOptions options1;
        private readonly AppInfoOptions options2;
        private readonly AppInfoOptions options3;

        public DefaultController(IOptions<AppInfoOptions> options
            , IOptionsSnapshot<AppInfoOptions> optionsSnapshot
            , IOptionsMonitor<AppInfoOptions> optionsMonitor)
        {
            options1 = options.Value;
            options2 = optionsSnapshot.Value;
            options3 = optionsMonitor.CurrentValue;
        }

        /// <summary>
        /// Get方法
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        //[ApiDescriptionSettings("Group1", "Group3")]
        public string Get()
        {
            //var options1 = App.GetOptions<AppInfoOptions>();
            var info1 = $@"名称：{options1.Name}，
                      版本：{options1.Version}，
                      公司：{options1.Company}";

            //var options2 = App.GetOptionsSnapshot<AppInfoOptions>();
            var info2 = $@"名称：{options2.Name}，
                      版本：{options2.Version}，
                      公司：{options2.Company}";

            //var options3 = App.GetOptionsMonitor<AppInfoOptions>();
            var info3 = $@"名称：{options3.Name}，
                      版本：{options3.Version}，
                      公司：{options3.Company}";

            return $"{info1}-{info2}-{info3}";
        }


        /// <summary>
        /// 异常检验方法
        /// </summary> 
        /// <returns></returns>
        [HttpGet]
        [Route("Exc1")]
        //[ApiDescriptionSettings("Group2")]
        public int Exc1()
        {
            //var s = int.Parse("sdfdsf");

            throw Oops.Oh(ErrorCodes.z1000, 12, 1909);
           
            return 1;
        }

        /// <summary>
        /// 异常检验方法
        /// </summary> 
        /// <returns></returns>
        [HttpGet]
        [Route("Exc2")]
        //[ApiDescriptionSettings("Group2")]
        public int Exc2(int c)
        {
            if (c < 3)
            {
                //throw Oops.Oh($"{c} 不能小于3", typeof(InvalidOperationException));
                throw Oops.Oh(ErrorCodes.z1000, c, 1909);

                //throw Oops.Oh(1000);
                //throw Oops.Oh(ErrorCodes.x1000);
                //throw Oops.Oh("哈哈哈哈");
                //throw Oops.Oh(errorCode: "x1001");
                //throw Oops.Oh(1000, typeof(Exception));
                //throw Oops.Oh(1000).StatusCode(400);    // 设置错误码



            }
            return c;
        }

    }
}
